package com.base.cn.platform.os.controller.qa.qatype;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.qa.qatype.QaType;
import com.base.cn.platform.os.entity.qa.qatype.QaTypeCondition;
import com.base.cn.platform.os.service.manage.teacher.SysTeacherService;
import com.base.cn.platform.os.service.qa.qatype.QaTypeBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 问答分类Controller
 *
 * @author LIZHIPENG
 * @since 2018/4/18
 */
@RestController
@RequestMapping("/course")
public class QaTypeController extends BaseController {

    @Autowired
    private QaTypeBiz qaTypeBiz;
    @Autowired
    private SysTeacherService sysTeacherService;

    /**
     * 查询问答分类数量
     * @param condition
     * @return
     */
    @RequestMapping("/qaTypeCount")
    public BigDecimal count(@RequestBody QaTypeCondition condition){
        return qaTypeBiz.count(condition);
    }

    /**
     * 分页查询问答分类
     * @param currentPage
     * @param condition
     * @return
     */
    @RequestMapping("/queryQaTypeListPage")
    public PageInfo<QaType> queryQaTypeListPage(@RequestParam(value = "currentPage", required = false, defaultValue = "1")int currentPage,
                                            @RequestBody QaTypeCondition condition){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        return qaTypeBiz.queryQaTypeListPage(page, condition);
    }

    /**
     * 多条件查询问答分类
     * @param condition
     * @return
     */
    @RequestMapping("/queryQaTypeList")
    public List<QaType> queryQaTypeList(@RequestBody QaTypeCondition condition){
        return qaTypeBiz.findQaTypeList(condition);
    }

    /**
     * 根据ID查询问答分类
     * @param qaTypeId 分类ID
     * @return
     */
    @RequestMapping("/findQaTypeById")
    public QaType findQaTypeById(@RequestParam("qaTypeId") BigDecimal qaTypeId){
        return qaTypeBiz.findQaTypeById(qaTypeId);
    }

    /**
     * 保存问答分类
     * @param qaType
     * @return
     */
    @RequestMapping("/saveOrUpdateQaType")
    public Map<String, Object> saveOrUpdateQaType(@RequestBody QaType qaType){
        if(ObjectUtils.isNotEmpty(qaType)){
            if(StringUtils.isEmpty(qaType.getTypeName()) || StringUtils.isEmpty(qaType.getTypeName().trim())){
                return ResultUtil.ERROR("分类名称不能为空");
            }
            /*if(StringUtils.isEmpty(qaType.getImageJson())){
                return ResultUtil.ERROR("请选择类别图标");
            }*/
            return qaTypeBiz.saveOrUpdateQaType(qaType);
        }
        return ResultUtil.ERROR();
    }

    /**
     * 修改问答分类状态
     * @param qaTypeIds 分类ID串
     * @param status 状态：1.正常 2.禁用 3.删除
     * @return
     */
    @RequestMapping("/updateQaTypeStatus")
    public Map<String, Object> updateQaTypeStatus(@RequestParam("qaTypeIds")String qaTypeIds,
                                                  @RequestParam("status")Integer status){
        return qaTypeBiz.updateQaTypeStatus(qaTypeIds, status);
    }

    /**
     * 修改问答分类的老师数量
     * @param newQaTypeId
     * @param oldQaTypeId
     */
    @RequestMapping("/updateTeacherNum")
    public void updateTeacherNum(@RequestParam(value = "newQaTypeId", required = false) BigDecimal newQaTypeId,
                                 @RequestParam(value = "oldQaTypeId", required = false) BigDecimal oldQaTypeId){
        qaTypeBiz.updateTeacherNum(newQaTypeId, oldQaTypeId);
    }

    /**
     * 通过老师ID，获取老师对象的问答类型对象
     * @param teacherId 老师ID
     * @return QaType
     */
    @RequestMapping("/findQaTypeByTeacherId")
    public QaType findQaTypeByTeacherId(@RequestParam("teacherId") BigDecimal teacherId){
        Map<String,Object> teacher = sysTeacherService.findSysTeacherById(teacherId,false,false,false,false,false);
        if(ObjectUtils.isNotEmpty(teacher)){
            BigDecimal id = new BigDecimal(teacher.get("qaTypeId").toString());
            return qaTypeBiz.findById(id);
        }
        return null;
    }
}
